package com.huiren.ui.configUI;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.huiren.util.JdbcUtil;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTable;

@Component
public class SqlConfigJPanel extends JPanel {

	private static Logger logger = Logger.getLogger(SqlConfigJPanel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private JDBCConfigJPanel jdbcConfigJPanel;
	private JTextArea goods_sql;
	private JTextArea supplier_sql;
	private JTextArea warehouse_sql;
	private DateTableModel data_model;
	private JTable data_table;

	public SqlConfigJPanel() {
		logger.info("初始化.");
		setLayout(new CardLayout(0, 0));

		JPanel panel = new JPanel();
		JScrollPane js = new JScrollPane(panel);
		add(js, "name_24451418464680");
		js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		panel.setLayout(null);
		JLabel lblNewLabel = new JLabel(
				"<html>药  品:查询字段 fid(药品id), fnumber(药品代码),fname(药品名称), fmodel(药品规格), funit(药品单位), approve_no(批准文号), factoryNumber(产地代码), factory(产地名称), helpcode(助剂码)</html>");
		lblNewLabel.setBounds(32, 21, 639, 36);
		panel.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("药  库:查询字段 fnumber(药库代码),name(药库名称)");
		lblNewLabel_1.setBounds(32, 222, 639, 15);
		panel.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("供应商:查询字段 fnumber(供应商代码),name(供应商名称)");
		lblNewLabel_2.setBounds(32, 131, 639, 15);
		panel.add(lblNewLabel_2);

		goods_sql = new JTextArea();
		goods_sql.setLineWrap(true);
		goods_sql.setBounds(32, 67, 718, 54);
		panel.add(goods_sql);

		supplier_sql = new JTextArea();
		supplier_sql.setLineWrap(true);
		supplier_sql.setBounds(32, 156, 718, 56);
		panel.add(supplier_sql);

		warehouse_sql = new JTextArea();
		warehouse_sql.setLineWrap(true);
		warehouse_sql.setBounds(32, 247, 718, 54);
		panel.add(warehouse_sql);

		data_model = new DateTableModel();
		data_table = new JTable(data_model);
		data_table.setBounds(32, 455, 600, 54);
		data_table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		JPanel dataPanel = new JPanel();
		dataPanel.setBounds(32, 322, 718, 204);
		panel.add(dataPanel);
		dataPanel.setLayout(new CardLayout(0, 0));
		JScrollPane warehouse_scroll = new JScrollPane(data_table);
		dataPanel.add(warehouse_scroll, "name_24451418464681");

		JButton test_goods = new JButton("测试");
		test_goods.setBounds(693, 34, 57, 23);
		panel.add(test_goods);
		test_goods.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				List<Map<String, Object>> goods = JdbcUtil
						.executeQuery(goods_sql.getText());
				List<List<Object>> goodslist = convert(goods);
				updateTable(goodslist);
			}
		});
		JButton test_supplier = new JButton("测试");
		test_supplier.setBounds(693, 127, 57, 23);
		panel.add(test_supplier);

		test_supplier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				List<Map<String, Object>> supplier = JdbcUtil
						.executeQuery(supplier_sql.getText());
				List<List<Object>> supplierlist = convert(supplier);
				updateTable(supplierlist);
			}
		});

		JButton test_warehouse = new JButton("测试");
		test_warehouse.setBounds(693, 218, 57, 23);
		panel.add(test_warehouse);
		test_warehouse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				List<Map<String, Object>> warehouse = JdbcUtil
						.executeQuery(warehouse_sql.getText());
				List<List<Object>> warehouselist = convert(warehouse);
				updateTable(warehouselist);
			}
		});
		JButton reset = new JButton("重置");
		reset.setBounds(245, 536, 57, 23);
		panel.add(reset);
		reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				reset();
			}
		});

		JButton save = new JButton("保存");
		save.setBounds(421, 536, 57, 23);
		panel.add(save);
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				save();
			}
		});
		reset();
	}

	// 重置所有sql
	private void reset() {
		List<Map<String, Object>> sqls = JdbcUtil
				.executeQuery("select * from T_YJ_CONFIG where key like 'select%'");
		for (Map<String, Object> sql : sqls) {
			if (sql.get("key").equals("selectgoods"))
				goods_sql.setText(sql.get("value").toString());
			else if (sql.get("key").equals("selectsupplier"))
				supplier_sql.setText(sql.get("value").toString());
			else if (sql.get("key").equals("selectwarehouse"))
				warehouse_sql.setText(sql.get("value").toString());
		}
	}

	// 保存sql
	private void save() {
		List<String> sqls = new ArrayList<String>();
		sqls.add("update T_YJ_CONFIG set value='" + goods_sql.getText()
				+ "'   where  key='selectgoods'");
		sqls.add("update T_YJ_CONFIG set value='" + supplier_sql.getText()
				+ "'   where  key='selectsupplier'");
		sqls.add("update T_YJ_CONFIG set value='" + warehouse_sql.getText()
				+ "'   where  key='selectwarehouse'");
		JdbcUtil.executeUpdate(sqls);
	}

	// 更新table数据
	private void updateTable(List<List<Object>> datalist) {
		data_model.setData(datalist);
		data_model.fireTableStructureChanged();
		data_table.validate();
		data_table.updateUI();
	}

	// 数据转化为table数据
	private List<List<Object>> convert(List<Map<String, Object>> mapData) {
		List<List<Object>> data = null;
		if (mapData != null)
			for (Map<String, Object> map : mapData) {
				List<Object> tempList = new ArrayList<Object>();
				List<Object> titleList = new ArrayList<Object>();
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					titleList.add(entry.getKey());
					tempList.add(entry.getValue());
				}
				if (data == null) {
					data = new ArrayList<List<Object>>();
					data.add(titleList);
				}
				data.add(tempList);
			}
		return data;
	}

	@PostConstruct
	public void initMethod() {

	}
}
